import { createWebHistory, createRouter } from 'vue-router'
import store from '@/store'




//1、创建路由
const history = createWebHistory()

//2、配置路由逻辑
const router = createRouter({
    history:history,
    routes:[
        {
            //首页容器
            path:"/",
            component:()=>import('@/pages/home/index'),
            children:[
                {
                  //答题系统首页(练习,选择练习科目)
                  path:'/',
                  name:'home',
                //   meta:{requireAuthentication:true},
                  component:()=> import('@/pages/home/MainPage')

                },{
                  
                    path:'/knowledgePoints',
                    component:()=>import('@/pages/home/KnowledgePointsPage')
                },
                {
                  //错题本，笔记本
                  path:'/noteBook',
                  component:()=>import('@/pages/home/NoteBook')
                },
                {
                  //选择树，用于题目/知识点分类选择
                    path:'/selectTreePage',
                    component:()=>import('@/pages/home/SelectTreePage')
                },
                {
                  //答题页
                  path:'/exercises',
                  component:()=>import('@/pages/home/exercisesPage')
                },
                {
                  path:'/myInformation',
                  component:()=>import('@/pages/home/MyInformationPage')

                }                

            ]
        },
        {
            //登录页
            path:"/login",
            name:'Login',
            component:() => import('@/pages/Login')

        }
    ]
})



/***
 * 全局路由拦截器
 * 用于实现在路由元数据中配置了需要认证的页面，
 * 如未检测到授权信息（默认未JWT令牌），或者授权
 * 超时，转向登录页面。
 * 登陆后跳转回原路由页面继续
 * 
 */
 router.beforeEach((to, from, next) => {
    if (to.meta.requireAuthentication && !store.getters['user/isAuthorized']) {
      // 未登陆，则直接转到登陆
      next({name: 'Login', query: {redirect: to.fullPath}})
    } else {
      // 已登陆，但不是管理员，则转到首页
      if (to.meta.requireAdministrator && !store.getters['user/isAdministrator']) {
        next({path: '/'})
      } else {
        next()
      }
    }
  })

export default router